package com.tanhua.dubbo.api.impl;

import com.tanhua.dubbo.api.MessageApi;
import com.tanhua.dubbo.pojo.Comment;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService(version = "1.0.0")
public class MessageApiImpl implements MessageApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 通过type获取Comment表中数据
     *
     *
     * @param userId
     * @param page
     * @param pageSize
     * @param type
     * @return
     */
    @Override
    public List<Comment> queryCommentListByType(Long userId, Integer page, Integer pageSize, int type) {

        //通过类型查询数据库
        PageRequest pageRequest = PageRequest.of(page-1, pageSize,Sort.by(Sort.Order.desc("created")));
        Query query = Query.query(Criteria
                .where("commentType").is(type)
                .and("publishUserId").is(userId)
                .and("userId").ne(userId))
                    .with(pageRequest);
        List<Comment> comments = this.mongoTemplate.find(query, Comment.class);
        return comments;

    }

}
